import { WeaSearchGroup,WeaFormItem } from 'ecCom';
import { Button,Tooltip,Icon } from 'antd';
import { inject, observer} from 'mobx-react';
import {WeaSwitch} from 'comsMobx';
import * as mobx from 'mobx'
import * as API_LIST from '../apis/fnaReportBase'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class Condition extends React.Component{

    render(){
        const { condition,form,formHis,conditionHis,tabKey,formChar,conditionChar} = this.props.listStore;		
        const { isEcharform,onEnterSearch } = this.props;
        let isFormInit = "";        
        let conditions = [];
        let formParams = {};
        if(!isEcharform){
            if(tabKey == 0){
                isFormInit = form.isFormInit;
                conditions = condition;
                formParams = form.getFormParams();        
            } else if(tabKey == 1){
                isFormInit = formHis.isFormInit;
                conditions = conditionHis;
                formParams = formHis.getFormParams();
            }
        }else{
            isFormInit = formChar.isFormInit;
            conditions = conditionChar;
            formParams = formChar.getFormParams();
        }       

        let group = [];
        isFormInit && conditions.map((c,index) =>{
            let col = 2;
            if(c.title == getLabel(82544,'数据过滤')){
                col = 1;
            }
            let items = [];
            c.items.map((fields, idx) => {
                let hide = false;
                if(tabKey == 0){
                    hide = this.getHideStatus(formParams,idx);
                }
                //开启：则报表查询结果中统计的费用包含当前显示的预算单位及其下级单位的费用；
                //关闭：则报表查询结果中统计的费用仅为当前显示的预算单位本身的数值；
                let tips = "";
                if(fields.conditionType == 'SWITCH'){
                     const text = [<span>
                        {getLabel(82550,'开启：则报表查询结果中统计的费用包含当前显示的预算单位及其下级单位的费用；')}<br/>
                        {getLabel(82548,'关闭：则报表查询结果中统计的费用仅为当前显示的预算单位本身的数值；')}
                    </span>];

                    tips = [
                                <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@tnqv7v@Tooltip`} title={text} placement="bottomLeft">
                                    <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@8qvq1s@Tooltip_Icon`} type="question-circle" style={{marginLeft: 10,verticalAlign:'middle',color:"#FFB218"}}/>
                                </Tooltip>
                            ];
                }
                items.push({
                    com:(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@5v8tqk@${fields.domkey[0]}`}
                        label={`${fields.label}`}
                        labelCol={{span: `${fields.labelcol}`}}
                        wrapperCol={{span: `${fields.fieldcol}`}}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@osjlep@${fields.domkey[0]}`} fieldConfig={fields} form={isEcharform?formChar:tabKey==0?form:formHis} onChange={this.onChange} onEnterSearch={onEnterSearch}/>
                            {tips}
                        </WeaFormItem>),
                    colSpan:1,
                    hide:hide
                })
            });


            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@m38kvm@${index}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} col={col} />)
        });

        return <div>{group}</div>;
    }

    /**
     * 返回隐藏状态
     * 预算汇总 => 预算期间  (费用汇总表)
     * 年度科目 => 预算期间  (预算执行情况表)
     * @param  {[type]} formParams [description]
     * @param  {[type]} idx        [condition的下标]
     * @return {[type]}            [description]
     */
    getHideStatus(formParams,idx){
        let hide = false;
        const { pageType } = this.props.listStore;
        const { feeperiod } = this.props.listStore;
        if(pageType == 'CostSummary'){
            if(idx >= 5) hide = true;
            if(formParams.groupFeeperiod == '1'){//费用汇总表
                if (idx == 5) hide = false;
            }else if(formParams.groupFeeperiod == '2'){
                if (idx == 6) hide = false;
            }else if(formParams.groupFeeperiod == '3'){
                if (idx == 7) hide = false;
            }else if(formParams.groupFeeperiod == '4'){
                if (idx == 8) hide = false;
            }else if(formParams.groupFeeperiod == '-1'){
                if (idx == 9) hide = false;
            }
        }
        if(pageType == 'BudgetPerformanceStatement'){//预算执行情况表
            if(idx >= 3 && idx <= 6) hide = true;
            if(formParams.subjectType == '1'){
                if (idx == 3) hide = false;
            }else if(formParams.subjectType == '2'){
                if (idx == 4) hide = false;
            }else if(formParams.subjectType == '3'){
                if (idx == 5) hide = false;
            }else if(formParams.subjectType == '4'){
                if (idx == 6) hide = false;
            }
        }
        if(pageType == 'TotalBudget' || pageType == 'ExpenseBudgetRefinement'){//预算总额表,费用预算细化表
            if(idx >= 3 && idx <= 6) hide = true;
            if(feeperiod == '1'){
                if (idx == 3) hide = false;
            }else if(feeperiod == '2'){
                if (idx == 4) hide = false;
            }else if(feeperiod == '3'){
                if (idx == 5) hide = false;
            }else if(feeperiod == '4'){
                if (idx == 6) hide = false;
            }else{
                if (idx == 6) hide = false;
            }
        }

        return hide;
    }

    refreshEchar = (data) => {//图表浏览按钮选择时，图表刷新
        const {advSearchParam,doSearch} = this.props.listStore;
        let searchParam = Object.assign({},advSearchParam);
        searchParam.orgId = data.orgId.value;
        doSearch(searchParam);
    }

    onChange = (data) => {
        
        const {pageType,form,initInfo,advSearchParam} = this.props.listStore;
        /**
         * 预算科目 => 预算汇总  (预算总额表,费用预算细化表)
         */
        
        if(pageType == 'TotalBudget' || pageType == 'ExpenseBudgetRefinement'){//预算总额表,费用预算细化表
            let subjectIds = data.subjectIds;
            if(subjectIds != undefined){
                let subjectIdsValue = subjectIds.value;
                 API_LIST.getSubjectFeeperiod({'subjectIds':subjectIdsValue}).then(data =>{
                    let feeperiod = data.feeperiod;
                    const {setFeeperiod} = this.props.listStore;
                    setFeeperiod(data.feeperiod);
                 });   
            }
        }

        /**
        *预算总额表 图表联动
        */
        if((pageType == 'TotalBudget' || pageType == 'CostSummary')){//预算总额表,费用汇总表
            if(typeof(data.orgId) != 'undefined'){
                this.refreshEchar(data);
            }
        }
    }
}

const getAdButtons = (listStore,ecId) => {
    const {doSearch,setShowSearchAd,clearFormFields,tabKey} = listStore;
	return [
		(<Button ecId={ecId+'_Button@lbverc@Search'} type="primary" onClick={()=>{doSearch();setShowSearchAd(false);}}>{getLabel(197,'搜索')}</Button>),
		(<Button ecId={ecId+'_Button@y4ftft@Reset'} type="ghost" onClick={()=>{clearFormFields(tabKey);}}>{getLabel(2022,'重置')}</Button>),
		(<Button ecId={ecId+'_Button@mhce6y@Cancel'} type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(31129,'取消')}</Button>)
	];
}

export {Condition,getAdButtons}
